package log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LogrReaderServlet
 */
public class LogReaderServlet extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private final String LOG_FILE_PATH = "D://WRKSPC//logviewer1//output";
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LogReaderServlet() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void doGet(HttpServletRequest request,
	        HttpServletResponse response) throws ServletException, IOException {
	
		response.setContentType("text/plain"); // Set content type of the
											   // response so that jQuery knows
											   // what it can expect.
		response.setCharacterEncoding("UTF-8"); // You want world domination,
												// huh?
		response.getWriter().write(readFile(LOG_FILE_PATH)); // Write response
															// body.
	}
	
	private String readFile(String file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = null;
		StringBuilder stringBuilder = new StringBuilder();
		String ls = "<br>";// System.getProperty("line.separator");
		
		while ((line = reader.readLine()) != null) {
			stringBuilder.append(line);
			stringBuilder.append(ls);
		}
		
		return stringBuilder.toString();
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
	        HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
}
